package com.devunion.salon.core.query;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author spetrakovsky
 */
public class QueryUtils {

    public static String getInString(Collection values) {
        if (values == null || values.isEmpty()) {
            return "";
        } else {
            StringBuilder builder = new StringBuilder(" in (");
            builder.append(joinValues(values)).append(" )");
            return builder.toString();
        }
    }

    public static String joinValues(Collection values) {
        StringBuilder builder = new StringBuilder();
        for (Iterator it = values.iterator(); it.hasNext();) {
            Object obj = it.next();
            if (obj != null) {
                builder.append('\'').append(obj).append('\'');
            }
            if (it.hasNext()) {
                builder.append(',');
            }
        }
        return builder.toString();
    }

    public static String getQuery4Properties(String alias, NamedProperty[] propertieses) {
        StringBuilder sb = new StringBuilder();
        return getQuery4Properties(sb, alias, propertieses);
    }

    public static String getMultiIn(String inCondition, Collection values) {
        if (values != null && !values.isEmpty()) {
            StringBuilder sb = new StringBuilder(" ");
            boolean noFirst = false;
            for (Object unitId : values) {
                if (noFirst) {
                    sb.append(" or ");
                } else {
                    noFirst = true;
                }
                sb.append(unitId).append(inCondition);
            }
            return sb.toString();
        }
        return "";
    }

    public static String getQuery4Properties(StringBuilder sb, String alias, NamedProperty[] propertieses) {
        boolean first = true;
        for (NamedProperty property : propertieses) {
            if (!first) {
                sb.append(", ");
            } else {
                first = false;
            }
            sb.append(alias).append('.').append(property.getPropertyName());
        }
        return sb.toString();
    }

    public static Object[] resultToArray(Object object) {
        if (object instanceof Object[]) {
            return (Object[]) object;
        } else {
            return new Object[]{object};
        }
    }

    public static Map resultToMap(Collection result) {
        Map res = new HashMap();
        for (Object o : result) {
            put2map(res, resultToArray(o));
        }
        return res;
    }

    private static Object[] getSubArray(Object[] array, int beginIndex) {
        return getSubArray(array, beginIndex, array.length);
    }

    private static Object[] getSubArray(Object[] array, int beginIndex, int endIndex) {
        Object[] _array = new Object[endIndex - beginIndex];
        for (int i = beginIndex, j = 0; i < endIndex; i++, j++) {
            _array[j] = array[i];
        }
        return _array;
    }

    private static void put2map(Map res, Object[] arr) {
        if (arr.length > 1) {
            res.put(arr[0], getSubArray(arr, 1));
        } else {
            res.put(arr[0], arr[0]);
        }
    }

}

